/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "main.h"
#include "TIM2CH2.h"
#include "buttons.h"
#include "serial.h"


/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern uint8_t USARTBuffer[USARTBufferSize];
extern uint8_t TxCounter, TxBuffLen;

extern uint16_t ISR_type;
extern uint8_t buttonState, processButtonState;	// button processed, pressed down, released up
extern unsigned int userActive;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles RTC global interrupt request.
  * @param  None
  * @retval None
  */
void RTC_IRQHandler(void)
{
  if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  {
    /* Clear the RTC Second interrupt */
    RTC_ClearITPendingBit(RTC_IT_SEC);

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    ISR_type |= ISR_RTC_SEC;
  }
}

/*
  * @brief  This function handles RTC Alarm interrupt request.
  * @param  None
  * @retval None
*/
void  RTCAlarm_IRQHandler(void) {
	  if(RTC_GetITStatus(RTC_IT_ALR) != RESET)
	  {
		  /* Clear EXTI line17 pending bit */
	    EXTI_ClearITPendingBit(EXTI_Line17);

	    /* Check if the Wake-Up flag is set */
	    if(PWR_GetFlagStatus(PWR_FLAG_WU) != RESET)
	    {
	      /* Clear Wake Up flag */
	      PWR_ClearFlag(PWR_FLAG_WU);
	    }
	    /* Wait until last write operation on RTC registers has finished */
	    RTC_WaitForLastTask();
	    /* Clear RTC Alarm interrupt pending bit */
	    RTC_ClearITPendingBit(RTC_IT_ALR);
	    /* Wait until last write operation on RTC registers has finished */
	    RTC_WaitForLastTask();
	    ISR_type |= ISR_RTC_ALARM;
  }
}


/**
  * @brief  This function handles USARTx global interrupt request.
  * @param  None
  * @retval None
  */
void USART1_IRQHandler(void)
{
  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  {
    /* Send one byte to the transmit data register */
    USART_SendData(USART1, USARTBuffer[TxCounter++]);
    if(TxCounter == TxBuffLen) {
       	TxCounter = TxBuffLen = 0;
        /* Disable the USART1 Transmit interrupt */
        USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
        ISR_type |= ISR_USART_TX;	// Transfer Completed
      }
  }
}


void EXTI15_10_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line12) != RESET)
  {
	  DisableButton_IRQ(EXTI15_10_IRQn);
	  	  	  	  	  	  	    // Disable interrupt to avoid any debounce effects
	  ISR_type |= ISR_EXTI12; 	//add EXTI ISR to the list of ISRs to be processed by the main loop

	  if (buttonState == BT0_DOWN) {
		  buttonState=BT_UP;
	  } else {
		  buttonState=BT0_DOWN;
		  processButtonState = BT0_DOWN;
	  }
	  userActive = TRUE;	// User is interacting with us, so do not allow the CPU to sleep

    /* Clear the  EXTI line 12 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line12);
  }

  if(EXTI_GetITStatus(EXTI_Line13) != RESET)
  {
	  DisableButton_IRQ(EXTI15_10_IRQn);
	  	  	  	  	  	  	  	// Disable interrupt to avoid any debounce effects
	  ISR_type |= ISR_EXTI13; 	//add EXTI ISR to the list of ISRs to be processed by the main loop

	  if (buttonState == BT1_DOWN) {
		  buttonState=BT_UP;
	  } else {
		  buttonState=BT1_DOWN;
		  processButtonState = BT1_DOWN;
	  }

	  userActive = TRUE;		// User is interacting with us, so do not allow the CPU to sleep

    /* Clear the  EXTI line 13 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line13);
  }

  if(EXTI_GetITStatus(EXTI_Line14) != RESET)
  {
	  DisableButton_IRQ(EXTI15_10_IRQn);
	  	  	  	  	  	  	  	// Disable interrupt to avoid any debounce effects
	  ISR_type |= ISR_EXTI14; 	//add EXTI ISR to the list of ISRs to be processed by the main loop
	  if (buttonState == BT2_DOWN) {
		  buttonState=BT_UP;
	  } else {
		  buttonState=BT2_DOWN;
		  processButtonState = BT2_DOWN;
	  }

	  userActive = TRUE;		// User is interacting with us, so do not allow the CPU to sleep

    /* Clear the  EXTI line 14 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line14);
  }

  if(EXTI_GetITStatus(EXTI_Line15) != RESET)
  {
	  DisableButton_IRQ(EXTI15_10_IRQn);
	  	  	  	  	  	  	  	// Disable interrupt to avoid any debounce effects
	  ISR_type |= ISR_EXTI15; 	//add EXTI ISR to the list of ISRs to be processed by the main loop
	  if (buttonState == BT3_DOWN) {
		  buttonState=BT_UP;
	  } else {
		  buttonState=BT3_DOWN;
		  processButtonState = BT3_DOWN;
	  }

	  userActive = TRUE;		// User is interacting with us, so do not allow the CPU to sleep

    /* Clear the  EXTI line 15 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line15);
  }
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
	TimingDelay_Decrement();
}

/**
  * @brief  This function handles TIM2 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM2_IRQHandler(void)
{
  if(TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET)
  {
    /* Clear TIM2 Capture compare interrupt pending bit */
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
    TIM2_CC_Handler ();
  }
}

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */ 


/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
